home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / EGAVGA.SWG / 0185_Vesa and 320x200x256.pas < prev    next >
Pascal/Delphi Source File  |  1995-02-28  |  10KB  |  395 lines

  1. {CF> Does any one have code to do Vesa 320x200x256?  Also page flipping?
  2.  CF> And s' stuff? }
  3.  
  4.  
  5. {Here's my VESA unit}
  6.  
  7. {$A+,B-,D+,E+,F-,G+,I-,L+,N-,O-,P-,Q-,R-,S-,T-,V+,X+}
  8. {$M 1024,0,65536}
  9. Unit Vesa;
  10. Interface
  11. Uses Crt,Dos;
  12. Var
  13.   xMax,
  14.   yMax: word; { VERY important you set these upon init'ing }
  15. Type
  16.   tRGB = record R,G,B: byte; end;
  17.   tDAC = array[0..255] of tRGB;
  18. Const
  19.   { Standard text }
  20.   _40x25t        = $02;
  21.   _80x25t        = $03;
  22.   { Standard VGA }
  23.   _640x480x2     = $11;
  24.   _640x480x16    = $12;
  25.   _320x200x256   = $13;
  26.   { Standard VESA }
  27.   _640x400x256   = $100;
  28.   _640x480x256   = $101;
  29.   _800x600x16    = $102;
  30.   _800x600x256   = $103;
  31.   _1024x768x16   = $104;
  32.   _1024x768x256  = $105;
  33.   _1280x1024x16  = $106;
  34.   _1280x1024x256 = $107;
  35.   { Textmode modes for VESA }
  36.   _80x60t        = $108;
  37.   _132x25t       = $109;
  38.   _132x43t       = $10A;
  39.   _132x50t       = $10B;
  40.   _132x60t       = $10C;
  41.   { Pretty much standard VESA }
  42.   _320x200x32K   = $10D;
  43.   _320x200x64K   = $10E;
  44.   _320x200x16M   = $10F;
  45.   _640x480x32K   = $110;
  46.   _640x480x64K   = $111;
  47.   _640x480x16M   = $112;
  48.   _800x600x32K   = $113;
  49.   _800x600x64K   = $114;
  50.   _800x600x16M   = $115;
  51.   _1024x768x32K  = $116;
  52.   _1024x768x64K  = $117;
  53.   _1024x768x16M  = $118;
  54.   _1280x1024x32K = $119;
  55.   _1280x1024x64K = $11A;
  56.   _1280x1024x16M = $11B;
  57. Var
  58.   Current_bank: byte;
  59.   Pp: byte;
  60. Const
  61.   vCycle_direction: byte = 1;
  62.  
  63. {═══════════════════════════════════════════════════════════════════════════}
  64. Procedure Clearscreen(c: byte);
  65. procedure Line(X1,Y1,X2,Y2: Integer; Color: Byte);
  66. Procedure HLine(x,y,x2: integer; color: byte);
  67. Procedure VLine(x,y,y2: integer; color: byte);
  68. Procedure Circle(X,y,size: longint; color: byte);
  69. Procedure SwitchBank(bank: byte);
  70. Procedure PutPix(x,y: word; c: byte);
  71. Procedure Cycle(var vpTemp: tDAC; start,finish: Byte);
  72. Procedure LoadPal(fn: pathstr);
  73. Procedure SetColor(Color,r,g,b: Byte);
  74. Procedure GetColor(Color: byte; var R,G,B : Byte);
  75. Procedure SetPalette(var vPal: tDAC);
  76. Procedure GetPalette(var vPal: tDAC);
  77. procedure Rectangle(x1, y1, x2, y2 : word; Color : byte);
  78. {───────────────────────────────────────────────────────────────────────────}
  79. Function SetMode(mode: word): boolean; { VGA & VESA modes }
  80. Function GetMode(var mode: word): boolean;
  81. {═══════════════════════════════════════════════════════════════════════════}
  82.  
  83. Implementation
  84.  
  85. Procedure Cycle(var vpTemp: tDAC; start,finish: Byte);
  86. Var
  87.   count,
  88.   speed : Byte;
  89.   temp : tRGB;
  90. Begin
  91.   If vCycle_direction = 0 then Exit;
  92.  
  93.   For speed := 1 to Abs(vCycle_direction) do begin
  94.     { Forwards? }
  95.     If Abs(vCycle_direction) = vCycle_direction then begin
  96.       temp := vpTemp[start];
  97.       for count := start to finish-1 do
  98.         vpTemp[count] := vpTemp[count+1];
  99.       vpTemp[finish] := temp;
  100.     end
  101.     { Backwards? }
  102.     else begin
  103.       temp := vpTemp[finish];
  104.       for count := finish downto start+1 do
  105.         vpTemp[count] := vpTemp[count-1];
  106.       vpTemp[start] := temp;
  107.     End;
  108.   End;
  109.  
  110.   Setpalette(vpTemp);
  111. End;
  112.  
  113. procedure Rectangle(x1,y1,x2,y2: word; Color: byte);
  114. begin
  115.   Line(x1,y1,x2,y1,Color);
  116.   Line(x2,y1,x2,y2,Color);
  117.   Line(x2,y2,x1,y2,Color);
  118.   Line(x1,y2,x1,y1,Color);
  119. end;
  120.  
  121. Procedure SetPalette(var vPal: tDAC);
  122. Var loop: byte;
  123. Begin
  124.   For loop := 0 to 255 do with vPal[loop] do SetColor(loop,r,g,b);
  125. End;
  126.  
  127. Procedure GetPalette(var vPal: tDAC);
  128. Var loop: byte;
  129. Begin
  130.   For loop := 0 to 255 do with vPal[loop] do GetColor(loop,r,g,b);
  131. End;
  132.  
  133. Procedure SetColor(color,r,g,b: Byte); Assembler;
  134. Asm
  135.   mov  dx, 3C8h   { Color port }
  136.   mov  al, color  { Number of color to change }
  137.   out  dx, al
  138.   inc  dx         { Inc dx to write }
  139.   mov  al, r      { Red value }
  140.   out  dx, al
  141.   mov  al, g      { Green }
  142.   out  dx, al
  143.   mov  al, b      { Blue }
  144.   out  dx, al
  145. End;
  146.  
  147. Procedure GetColor(Color: byte; var r,g,b: byte); Assembler;
  148. { This reads the values of the Red, Green and Blue DAC values of a
  149.   certain color and returns them to you in r (red), g (green), b (blue) }
  150. asm
  151.   mov  dx, 3C7h
  152.   mov  al, color
  153.   out  dx, al
  154.   add  dx, 2
  155.   in   al, dx
  156.   les  di, r
  157.   stosb
  158.   in   al, dx
  159.   les  di, g
  160.   stosb
  161.   in   al, dx
  162.   les  di, b
  163.   stosb
  164. end;
  165.  
  166. Procedure Circle(X,Y,size: longint; color: byte);
  167. Var Xl,Yl : LongInt;
  168. Begin
  169.   If Size=0 Then Begin
  170.     PutPix(X,Y,color);
  171.     Exit;
  172.   End;
  173.   Xl := 0;
  174.   Yl := Size;
  175.   Size := Size*Size+1;
  176.   Repeat
  177.     PutPix(X+Xl,Y+Yl,color);
  178.     PutPix(X-Xl,Y+Yl,color);
  179.     PutPix(X+Xl,Y-Yl,color);
  180.     PutPix(X-Xl,Y-Yl,color);
  181.     If Xl*Xl+Yl*Yl >= Size Then Dec(Yl)
  182.     Else Inc(Xl);
  183.   Until Yl = 0;
  184.   PutPix(X+Xl,Y+Yl,color);
  185.   PutPix(X-Xl,Y+Yl,color);
  186.   PutPix(X+Xl,Y-Yl,color);
  187.   PutPix(X-Xl,Y-Yl,color);
  188. end;
  189.  
  190. Procedure HLine(x,y,x2: integer; color: byte);
  191. Begin
  192.   for x := x to x2 do putpix(x,y,color);
  193. End;
  194.  
  195. Procedure VLine(x,y,y2: integer; color: byte);
  196. Begin
  197.   for y := y to y2 do putpix(x,y,color);
  198. End;
  199.  
  200. procedure Line(X1, Y1, X2, Y2: Integer; Color: Byte);
  201. var X, Y, Dx, Dy, Xs, Ys, Direction: Integer;
  202. begin
  203.   if x1 = x2 then hline(x1,y1,y2,color)
  204.   else if y1 = y2 then vline(x1,y1,x2,color)
  205.   else begin
  206.     X := X1; Y := Y1; Xs := 1; Ys := 1;
  207.     if X1 > X2 then Xs := -1;
  208.     if Y1 > Y2 then Ys := 01;
  209.     Dx := Abs(X2 - X1); Dy := Abs(Y2 - Y1);
  210.     if Dx = 0 then direction := -1
  211.     else Direction := 0;
  212.     while not ((X = X2) and (Y = Y2)) do begin
  213.       PutPix(X,Y,Color);
  214.       if Direction < 0 then begin                               
  215.         Inc(Y,Ys);
  216.         Inc(Direction,Dx);
  217.       end 
  218.       else begin
  219.         Inc(x,Xs);
  220.         Dec(Direction,Dy);
  221.       end;
  222.     end;
  223.   end;
  224. end;  { Line }
  225.  
  226. Function GetMode(var mode: word): boolean; assembler;
  227. asm
  228.   Mov  ax, 4F03h
  229.   Int  10h
  230.   Mov  word ptr mode, bx
  231.   Cmp  Al, 4Fh
  232.   Je   @Yes
  233.   mov  al, false
  234.   Jmp  @end
  235.  @Yes:
  236.   mov  al, true
  237.  @End:
  238. end;
  239.  
  240. Function SetMode(mode: word): boolean; assembler;
  241. { This function will work for more than just VESA modes, and more than  }
  242. { Just VESA cards also.  If it's under $100 (where vesa modes begin) it }
  243. { will use the normal video bios instead. So people without VESA cards/ }
  244. { drivers still can use this for 320x200x256, etc.                      }
  245. asm
  246.   { Comment this part out if you want to use vesa for this }
  247.   {--}
  248.   Cmp Mode, 100h
  249.   Jb  @Normal_VGA { If it's below 100h then it's a std mode, why use VESA? }
  250.   {--}
  251.   Mov Ax, 4F02h   { VESA set modes }
  252.   Mov Bx, mode
  253.   Int 10h
  254.   Cmp Ax, 004Fh   { AL=4F VESA supported, AH=00 successful }
  255.   Jne @Error      { Else Error }
  256.   mov al, true
  257.   jmp @done
  258.  @Error:
  259.   mov al, false
  260.   Jmp @done
  261.  @Normal_VGA:
  262.   mov ax, mode    { AH will of course be zero, as intended }
  263.   int 10h
  264.   Mov al, true
  265.  @done:
  266. end;
  267.  
  268. Procedure SwitchBank(bank: byte); Assembler;
  269. Asm
  270.   Mov al, bank
  271.   Cmp Current_bank, al
  272.   je @End
  273.   Mov Current_bank, al
  274.   Mov Ax, 4F05h
  275.   Xor Bx, Bx
  276.   Adc Dx, 0
  277.   Mov Dl, bank
  278.   Int 10h
  279.  @End:
  280. End;
  281.  
  282. Procedure Clearscreen(c: byte);
  283. var loop: byte;
  284. begin
  285.   for loop := 0 to (longint(xmax)*ymax) div $FFFF do begin
  286.     switchbank(loop);
  287.     Fillchar(mem[SegA000:0],$FFFF,c);
  288.     Fillchar(mem[SegA000:$FFFF],$1,c);
  289.   end;
  290. end;
  291.  
  292. Procedure LoadPal(Fn: PathStr);
  293. Var
  294.   DAC: tDAC;
  295.   F: file;
  296.   Loop: integer;
  297. Begin
  298.   Assign(f,Fn);
  299.   Reset(f,1);
  300.   If ioresult <> 0 then exit;
  301.   BlockRead(f,DAC,Sizeof(DAC));
  302.   Close(f);
  303.   for Loop := 0 to 255 do with dac[loop] do SetColor(Loop,r,g,b);
  304. end;
  305.  
  306. Procedure PutPix(x,y: word; c: byte); assembler;
  307. Asm
  308.   { Do some simple checking }
  309.   mov  ax, x
  310.   cmp  xmax,ax
  311.   jb   @end
  312.  
  313.   mov  ax, y
  314.   cmp  ymax, ax
  315.   jb   @end
  316.   
  317.   dec  x
  318.  
  319.   { Calculate where we're going to place the pixel at A000:???? }
  320.   Mov  ES, SegA000
  321.   Mov  AX, Ymax
  322.   Mul  pp
  323.   Add  Ax, Y
  324.   Mov  Bx, Ax
  325.   Mov  Ax, Xmax
  326.   Mul  Bx
  327.   Add  Ax, X
  328.   Adc  Dx, 0
  329.   Mov  Di, Ax
  330.   Cmp  Dl, Current_bank
  331.   { If we're at the bank we need to be, then skip it }
  332.   Je   @skip
  333.   { Set the video bank to what we need }
  334.   Mov  Current_bank, Dl
  335.   Mov  Ax, 4F05h
  336.   Xor  Bx, Bx
  337.   Int  10h
  338.  
  339.  @Skip:
  340.   Mov  Al, C
  341.   Mov  Es:[Di], Al
  342.  @End:
  343. End;
  344.  
  345. End.
  346.  
  347. ... How do blonds get minks?  The same way Minks get Minks!
  348. --- Blue Wave/Max v2.12 [NR]
  349.  * Origin: Infinity Complex -= 28.8k =- (613)549-7847 (1:249/153)
  350. SEEN-BY: 12/12 163/99 211 167/90 221/100 224/25 240/99 241/99
  351. SEEN-BY: 243/3 244/99 246/1 249/1 99 100 101 112 127 128 152
  352. SEEN-BY: 249/153 200 201 396/1 3615/50 51
  353. PATH: 249/153 100 99 12/12 3615/50
  354.                                        
  355. {SWAG=???.SWG,JOHN STEPHENSON,Vesa and 320x200x256 3/3}
  356. MSGID: 1:249/153.0 2efc50b2
  357. {CF> Does any one have code to do Vesa 320x200x256?  Also page flipping? 
  358.  CF> And s' stuff? 
  359.  
  360.  Lastly, an example:}
  361.  
  362. uses crt,vesa,asmmisc;
  363. var
  364.   loop: word;
  365.   vpTemp: tDac;
  366.   pixels : word;
  367.   hx,hy: longint;
  368.  
  369. begin
  370.   xmax := 320;
  371.   ymax := 200;
  372.   setmode(_320x200x256);
  373.   LoadPal('TUNNEL.PAL'); { Get your own palette! }
  374.   GetPalette(vpTemp);
  375.   
  376.   { Calculate the amount of pixels to 1,1 from xmax div 2,ymax div 2 using }
  377.   { the pythagorean theorm }
  378.   hy := ymax div 2; { Centre Y }
  379.   hx := xmax div 2; { Centre X }          {       _____ }
  380.   pixels := round(sqrt((hx*hx)+(hy*hy))); { c := √a²+b² }
  381.  
  382.   for loop := 0 to pixels do begin
  383.     circle(xmax div 2,ymax div 2,loop,loop mod 255+1);
  384.     Cycle(vpTemp,1,255);
  385.   end;
  386.   while keypressed do readkey;
  387.   { Don't rotate black! }
  388.   while not keypressed do begin
  389.     Retrace;
  390.     Cycle(vpTemp,1,255);
  391.   end;
  392.   readkey;
  393.   setmode(lastmode);
  394. end.
  395.